<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>书籍信息表</title>
    <link rel="stylesheet" href="http://47.100.102.177:8080/app/css/common/bootstrap.min.css">
    <link rel="stylesheet" href="../../../boot/plugin/download/font_2801350_hw9pyy8c125/iconfont.css">    <script src="http://47.100.102.177:8080/app/js/common/jquery-1.12.4.js"></script>
    <!-- 必须先引入vue，  后使用element-ui -->
    <script src="../../../boot/plugin/element/vue.js"></script>
    <!--引入 element-ui 的样式，-->
    <link rel="stylesheet" href="../../../boot/plugin/element/element.css">
    <!-- 引入element 的组件库-->
    <script src="../../../boot/plugin/element/element.js"></script>
    <style>
        th {
            text-align: center;
        }
        
        .bg,
        .bg-add {
            width: 100%;
            height: 100vh;
            position: absolute;
            top: 0px;
            background-color: rgba(0, 0, 0, 0.2);
            display: none;
        }
        
        .inf {
            background-color: white;
            height: 630px;
            width: 45%;
            margin: auto;
            margin-top: 60px;
            border-radius: 3px;
        }
        
        .inf-head {
            width: 100%;
            height: 30px;
            background-color: rgb(21, 184, 215);
            border-radius: 3px;
        }
        
        #close,
        #close-add {
            float: right;
            margin-right: 5px;
            background-color: red;
            color: white;
            font-weight: 900;
            border-radius: 5px;
            border: 0px;
            margin-top: 3px;
        }
        
        .inf-from {
            width: 100%;
            height: 600px;
            border-radius: 3px;
            overflow: scroll;
        }
        
        .mailTable,
        .mailTable1 {
            width: 100%;
            border-top: 1px solid #E6EAEE;
            border-left: 1px solid #E6EAEE;
            margin-bottom: 30px;
        }
        
        .mailTable tr td,
        .mailTable1 tr td {
            width: 200px;
            height: 35px;
            line-height: 35px;
            box-sizing: border-box;
            padding: 0 10px;
            border-bottom: 1px solid #E6EAEE;
            border-right: 1px solid #E6EAEE;
        }
        
        .mailTable tr td.column,
        .mailTable1 tr td.column1 {
            background-color: #EFF3F6;
            color: #393C3E;
            width: 30%;
        }
        .img:hover{
            transform:scale(3,3)
        }
    </style>
</head>

<body>
    <div style="width: 100%;height: 20px;"></div>
    <form id="form_book" class="form-inline" style="text-align: center;">
        <div class="form-group">
            <label for="bookName">书名</label>
            <input type="text" class="form-control" name="title" id="bookName" placeholder="请输入书名">
            <label for="author">作者</label>
            <input type="text" class="form-control" name="author" id="author" placeholder="请输入作者">
            <label for="bookType">书籍类别</label>
            <select id="bookType" name="category" style="width: 196px;height: 34px;border-radius: 3px;border: 1px solid #ccc">
                <option selected></option>
                <option>艺术</option>
                <option>文学</option>
                <option>动漫</option>
                <option>经管</option>
                <option>童书</option>
                <option>人文</option>
                <option>励志</option>
                <option>生活</option>
                <option>科技</option>
                <option>社会</option>
            </select>
        </div>
        <button type="button" class="btn btn-default" onclick="searchBook()"><i class="iconfont"
                style="font-size: 14px;color: #3E8EF7;">&#xe660;</i></button>
        <button type="button" style="margin-left: 10px;" class="btn btn-info" onclick="add()">新增</button>
        <button type="button" style="margin-left: 10px;" class="btn btn-danger" onclick="fun1()">批量删除</button>
    </form>
    <div style="width: 100%;height: 20px;"></div>
    <table class="table table-bordered" style="text-align: center;">
        <thead id="th">
            <tr>
                <th><input type="checkbox" id="all"></th>
                <th>书籍id</th>
                <th>书名</th>
                <th>示例图</th>
                <th>出版社</th>
                <th>出版时间</th>
                <th>作者</th>
                <th>单价(双击修改)</th>
                <th>类别</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tb">

        </tbody>
    </table>
    <div style="float: right;margin-right: 20px;">
        <div style="float: left;height: 80px;line-height: 80px;margin-right: 20px;" id="pageNum">共&nbsp;<span style="color: red">${haveBooks}</span>&nbsp;条记录&nbsp;&nbsp;</div>
        <div style="float: left;height: 80px;line-height: 80px;margin-right: 20px;">&nbsp;<span style="color: red">7</span>&nbsp;条&nbsp;/&nbsp;页</div>
        <div id="app" style="float: left;" onclick="page()">
            <el-pagination background layout="prev, pager, next" :total="${allBooks}*10" style="margin-top: 23px;">
            </el-pagination>
        </div>
    </div>
    <div class="bg" style="position: fixed;">
        <div class="inf">
            <div class="inf-head">
                <h4 style="color: white;display: inline;line-height: 30px;margin-left: 5px;">书籍详细信息</h4>
                <input type="button" id="close" value="×">
            </div>
            <div class="inf-from">
                <table class="mailTable" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="column">书籍id</td>
                        <td id="bookid_update"></td>
                    </tr>
                    <tr>
                        <td class="column">书名</td>
                        <td id="bookname_update"></td>
                    </tr>
                    <tr>
                        <td class="column">示例图</td>
                        <td id="picture_update"></td>
                    </tr>
                    <tr>
                        <td class="column">出版时间</td>
                        <td id="time_update"></td>
                    </tr>
                    <tr>
                        <td class="column">出版社</td>
                        <td id="compay_update"></td>
                    </tr>
                    <tr>
                        <td class="column">作者</td>
                        <td id="author_update"></td>
                    </tr>
                    <tr>
                        <td class="column">单价</td>
                        <td id="price_update"></td>
                    </tr>
                    <tr>
                        <td class="column">库存(双击修改)</td>
                        <td id="haveNum_update" ondblclick="update_stock(this)"></td>
                    </tr>
                    <tr>
                        <td class="column">类别</td>
                        <td id="booktype_update"></td>
                    </tr>
                    <tr>
                        <td class="column">书籍介绍</td>
                        <td id="bookint_update"></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <div class="bg-add" style="position: fixed;">
        <div class="inf">
            <div class="inf-head">
                <h4 style="color: white;display: inline;line-height: 30px;margin-left: 5px;">新增书籍</h4>
                <input type="button" id="close-add" value="×">
            </div>
            <div class="inf-from">
                <form action="" id="form_addBook">
                    <table class="mailTable1" cellspacing="0" cellpadding="0">
                        <tr>
                            <td class="column1">书名</td>
                            <td><input class="form-control" name="title" id="bookname_add" type="text" placeholder="请输入书名"></td>
                        </tr>
                        <tr>
                            <td class="column1">示例图url地址</td>
                            <td><input class="form-control" name="pictureUrl" id="bookUrl_add" type="text" placeholder="请输入书籍图片地址"></td>
                        </tr>
                        <tr>
                            <td class="column1">出版社</td>
                            <td><input class="form-control" type="text" name="compay" id="compay_add" placeholder="请输入出版社"></td>
                        </tr>
                        <tr>
                            <td class="column1">出版时间</td>
                            <td><input type="date" name="date" id="time_add" style="border-radius: 4px;border: 1px solid #999;"></td>
                        </tr>
                        <tr>
                            <td class="column1">作者</td>
                            <td><input class="form-control" name="author" id="author_add" type="text" placeholder="请输入作者"></td>
                        </tr>
                        <tr>
                            <td class="column1">价格</td>
                            <td><input class="form-control" name="price" id="price_add" type="text" placeholder="请输入价格"></td>
                        </tr>
                        <tr>
                            <td class="column1">库存</td>
                            <td><input class="form-control" name="stock" id="stock_add" type="text" placeholder="请输入库存"></td>
                        </tr>
                        <tr>
                            <td class="column1">类别</td>
                            <td>
                                <select class="form-control" name="category" id="booktype_add">
                                    <option selected></option>
                                    <option>艺术</option>
                                    <option>文学</option>
                                    <option>动漫</option>
                                    <option>经管</option>
                                    <option>童书</option>
                                    <option>人文</option>
                                    <option>励志</option>
                                    <option>生活</option>
                                    <option>科技</option>
                                    <option>社会</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="column1">书籍介绍</td>
                            <td><textarea class="form-control" name="detail" id="bookint_add" type="text" placeholder="请输入介绍书籍"></textarea></td>
                        </tr>
                    </table>
                    <button type="button" class="btn btn-default" style="float: left;margin-left: 180px;" onclick="addBook()">新增</button>
                    <button type="reset" class="btn btn-default" style="float: right;margin-right: 180px;">重置</button>
                </form>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        new Vue().$mount('#app');

        let book_list = ${books};
        let category = ${category};
        let tb = document.getElementById("tb");
        let str = "";
        for (let i = 0; i < book_list.length; i++) {
            str += '<tr>';
            str += '<td><input type="checkbox" name="cks"></td>';
            str += '<td>' + book_list[i].bkId + '</td>';
            str += '<td>' + book_list[i].title + '</td>';
            str += '<td><img class="img" src="' + book_list[i].pictureUrl + '" style="height: 50px;width: 50px;"></td>';
            str += '<td>' + book_list[i].compay + '</td>';
            str += '<td>' + book_list[i].date + '</td>';
            str += '<td>' + book_list[i].author + '</td>';
            str += '<td>' + book_list[i].price + '</td>';
            str += '<td>' + category[i] + '</td>';
            str += '<td>';
            str += '<button class="btn-link" type="button" onclick="search(this)">详情</button>';
            str += '<button class="btn-link" type="button" onclick="del(this)">删除</button>';
            str += '</td>';
            str += '</tr>';
        }
        tb.innerHTML = str;

        let len = document.getElementById("tb").getElementsByTagName("tr").length;
        let trs = document.getElementById("tb").getElementsByTagName("tr");
        for (let i = 0; i < len; i++) {
            let td = trs[i].querySelector('td:last-child').previousElementSibling.previousElementSibling;
            td.ondblclick = update_text;
        }

        function page() {
            let active = document.getElementsByClassName("active")[0];
            let param = "nowPage="+parseInt(active.innerText);
            $.get('/boot/admin/getNowBook',param,function (res){
                str = "";
                let allList = res.data;

                book_list = allList[0];
                category = allList[1];

                for (let i = 0; i < book_list.length; i++) {
                    str += '<tr>';
                    str += '<td><input type="checkbox" name="cks"></td>';
                    str += '<td>' + book_list[i].bkId + '</td>';
                    str += '<td>' + book_list[i].title + '</td>';
                    str += '<td><img class="img" src="' + book_list[i].pictureUrl + '" style="height: 50px;width: 50px;"></td>';
                    str += '<td>' + book_list[i].compay + '</td>';
                    str += '<td>' + book_list[i].date + '</td>';
                    str += '<td>' + book_list[i].author + '</td>';
                    str += '<td>' + book_list[i].price + '</td>';
                    str += '<td>' + category[i] + '</td>';
                    str += '<td>';
                    str += '<button class="btn-link" type="button" onclick="search(this)">详情</button>';
                    str += '<button class="btn-link" type="button" onclick="del(this)">删除</button>';
                    str += '</td>';
                    str += '</tr>';
                }
                tb.innerHTML = str;

                let len = document.getElementById("tb").getElementsByTagName("tr").length;
                let trs = document.getElementById("tb").getElementsByTagName("tr");
                for (let i = 0; i < len; i++) {
                    let td = trs[i].querySelector('td:last-child').previousElementSibling.previousElementSibling;
                    td.ondblclick = update_text;
                }
            });
        }

        function update_stock(obj){
            if (parseInt(${admin.powerId})==1 || parseInt(${admin.powerId})==2){
                let oldValue = obj.innerText;
                obj.innerHTML = "";
                let input = document.createElement("input");
                let td = obj;
                input.type = "text";
                input.style.width="35px";
                input.value = oldValue;
                input.onblur = function() {
                    td.innerHTML = this.value;
                    let bookId = parseInt(document.getElementById("bookid_update").innerText);
                    let stock = (this.value)*1;
                    let param = {bookId:bookId,stock:stock}
                    $.get('/boot/admin/updateBookStock',param,function (res){
                        if(res.code=="200"){
                            alert(res.msg);
                        }
                    })
                }
                input.onclick = function() {
                    this.select();
                }
                td.appendChild(input);
            }else {
                alert("对不起!只有Ⅰ,Ⅱ级管理员才能执行此操作!!!")
            }
        }

        function update_text() {
            if (parseInt(${admin.powerId})==1 || parseInt(${admin.powerId})==2){
                let oldValue = this.innerText;
                this.innerHTML = "";
                let input = document.createElement("input");
                let td = this;
                input.type = "text";
                input.style.width="35px";
                input.value = oldValue;
                input.onblur = function() {
                    td.innerHTML = this.value;
                    let bookId = parseInt(td.parentElement.firstChild.nextSibling.innerText);
                    let price = (this.value)*1;
                    let param = {bookId:bookId,price:price}
                    $.get('/boot/admin/updateBook',param,function (res){
                        if(res.code=="200"){
                            alert(res.msg);
                        }
                    })
                }
                input.onclick = function() {
                    this.select();
                }
                td.appendChild(input);
            }else {
                alert("对不起!只有Ⅰ,Ⅱ级管理员才能执行此操作!!!")
            }

        }

        function searchBook(){
            let form = $("#form_book");
            let param = form.serialize();
            str = "";
            $.post('/boot/admin/findBook',param,function (res){
                str = "";
                let allList = res.data;

                book_list = allList[0];
                category = allList[1];

                if (book_list!=null && book_list[0]!=null){
                    for (let i = 0; i < book_list.length; i++) {
                        str += '<tr>';
                        str += '<td><input type="checkbox" name="cks"></td>';
                        str += '<td>' + book_list[i].bkId + '</td>';
                        str += '<td>' + book_list[i].title + '</td>';
                        str += '<td><img class="img" src="' + book_list[i].pictureUrl + '" style="height: 50px;width: 50px;"></td>';
                        str += '<td>' + book_list[i].compay + '</td>';
                        str += '<td>' + book_list[i].date + '</td>';
                        str += '<td>' + book_list[i].author + '</td>';
                        str += '<td>' + book_list[i].price + '</td>';
                        str += '<td>' + category[i] + '</td>';
                        str += '<td>';
                        str += '<button class="btn-link" type="button" onclick="search(this)">详情</button>';
                        str += '<button class="btn-link" type="button" onclick="del(this)">删除</button>';
                        str += '</td>';
                        str += '</tr>';
                    }
                }else {
                    str += '<tr><td colspan="10">查询为空</td></tr>';
                }
                tb.innerHTML = str;

                let len = document.getElementById("tb").getElementsByTagName("tr").length;
                let trs = document.getElementById("tb").getElementsByTagName("tr");
                for (let i = 0; i < len; i++) {
                    let td = trs[i].querySelector('td:last-child').previousElementSibling.previousElementSibling;
                    td.ondblclick = update_text;
                }

            })
        }

        function addBook(){
            let form = $("#form_addBook");
            let param = form.serialize();
            str = "";
            $.post('/boot/admin/addBook',param,function (res){
                if (res.code=="200"){
                    alert(res.msg);
                    location.reload();
                }
            })
        }

        let tbody = document.getElementById("tb");

        function fun1() {
            if (parseInt(${admin.powerId})==1 || parseInt(${admin.powerId})==2){
                var flag = confirm("您确定要执行批量删除操作吗?");
                if (flag) {
                    //判断是否有选中的.
                    let count = 0;
                    let cks = document.getElementsByName("cks");
                    for (let i = 0; i < cks.length; i++) {
                        if (cks[i].checked) {
                            count++;
                        }
                    }
                    if (count == 0) {
                        alert("sorry,请你先勾选删除的数据!");
                        return;
                    }

                    //搞定勾选的 - 删除...
                    for (let i = cks.length - 1; i >= 0; i--) {
                        if (cks[i].checked) {
                            let bookId = cks[i].parentElement.nextSibling.innerText;
                            let param = "delBookId="+parseInt(bookId);
                            $.get('/boot/admin/delOneBook',param,function (res){
                            })
                        }
                    }
                    alert("删除成功");
                    location.reload();
                }
            }else {
                alert("对不起!只有Ⅰ,Ⅱ级管理员才能执行此操作!!!")
            }

        }

        function del(obj) {
            if (parseInt(${admin.powerId})==1 || parseInt(${admin.powerId})==2){
                let bookId = obj.parentElement.parentElement.firstChild.nextSibling.innerText;
                let flag = confirm("确定删除吗?");

                if (flag) {
                    let param = "delBookId="+parseInt(bookId);
                    $.get('/boot/admin/delOneBook',param,function (res){
                        if (res.code=="200"){
                            alert(res.msg);
                            location.reload();
                        }
                    })
                }
            }else {
                alert("对不起!只有Ⅰ,Ⅱ级管理员才能执行此操作!!!")
            }

        }

        function search(obj) {
            let bg = document.getElementsByClassName("bg");
            bg[0].style.display = "block";
            let tr = obj.parentElement.parentElement;
            let td_arr = tr.children;
            let bookid_update = document.getElementById("bookid_update");
            let bookname_update = document.getElementById("bookname_update");
            let picture_update = document.getElementById("picture_update");
            let time_update = document.getElementById("time_update");
            let compay_update = document.getElementById("compay_update");
            let author_update = document.getElementById("author_update");
            let price_update = document.getElementById("price_update");
            let haveNum_update = document.getElementById("haveNum_update");
            let booktype_update = document.getElementById("booktype_update");
            let bookint_update = document.getElementById("bookint_update");

            bookid_update.innerText = td_arr[1].innerText;
            bookname_update.innerText = td_arr[2].innerText;
            picture_update.innerHTML = td_arr[3].innerHTML;
            author_update.innerText = td_arr[6].innerText;
            time_update.innerText = td_arr[5].innerText;
            compay_update.innerText = td_arr[4].innerText;
            price_update.innerText = td_arr[7].innerText;
            booktype_update.innerText = td_arr[8].innerText;
            let param = "bookId="+parseInt(td_arr[1].innerText);
            $.get('/boot/admin/findOneBook',param,function (res){
                haveNum_update.innerText = res.stock;
                bookint_update.innerText = res.detail;
            })
        }

        function add() {
            if (parseInt(${admin.powerId})==1 || parseInt(${admin.powerId})==2){
                let bgadd = document.getElementsByClassName("bg-add");
                bgadd[0].style.display = "block";
            }else {
                alert("对不起!只有Ⅰ,Ⅱ级管理员才能执行此操作!!!")
            }
        }

        $(function() {
            //通过点击最上面的全选框，它的选中状态决定后面复选框的选中状态
            $("#all").click(function() {
                console.log($(this).prop("checked"));
                console.log($("#tb input"));
                $("#tb input").prop("checked", $(this).prop("checked"));
            });

            //后面的复选框点击后，判断是否全部都选中，如果都选中，最上面的全选框自动勾上
            $("#tb input").click(function() {
                console.log($("#tb input:checked")); //选中的元素
                if ($("#tb input:checked").length == $("#tb input").length) {
                    $("#all").prop("checked", true);
                } else {
                    $("#all").prop("checked", false);
                }
            });
            $("#close").click(function() {
                $(".bg").css("display", "none");
            });
            $("#close-add").click(function() {
                $(".bg-add").css("display", "none");
            });
            $("#bookName").bind("input propertychange", function() {
                var text = $("#bookName").val();
                if (text != '') { //若输入框1的内容不为空，则输入框2不可用
                    $("#author").attr("disabled", "disabled"); //设置输入框2不可用
                    $("#bookType").attr("disabled", "disabled");
                } else {
                    $("#author").removeAttr("disabled"); //移除不可用的属性
                    $("#bookType").removeAttr("disabled");
                }
            })
            $("#author").bind("input propertychange", function() {
                var text = $("#author").val();
                if (text != '') { //若输入框1的内容不为空，则输入框2不可用
                    $("#bookName").attr("disabled", "disabled"); //设置输入框2不可用
                    $("#bookType").attr("disabled", "disabled");
                } else {
                    $("#bookName").removeAttr("disabled"); //移除不可用的属性
                    $("#bookType").removeAttr("disabled");
                }
            })
            $("#bookType").bind("input propertychange", function() {
                var text = $("#bookType").val();
                if (text != '') { //若输入框1的内容不为空，则输入框2不可用
                    $("#bookName").attr("disabled", "disabled");
                    $("#author").attr("disabled", "disabled");
                } else {
                    $("#bookName").removeAttr("disabled"); //移除不可用的属性
                    $("#author").removeAttr("disabled");
                }
            })
        });
    </script>
</body>

</html>